package framework.states;

import java.util.Properties;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.StateBasedGame;

import framework.BasicActionGetterInstance;
import framework.C;
import ui.KeyBinding;
import ui.TextFieldKeyConfig;

public class InputConfigurator extends BackableState {
	
	private AngelCodeFont font;
	
	private TrueTypeFont fontTf = C.Font.SMALL_FONT;
	
	protected TextFieldKeyConfig[] player1Tf;
	
	protected TextFieldKeyConfig[] player2Tf;
	
	protected KeyBinding[] player1Bindings;
	
	protected KeyBinding[] player2Bindings;

	protected TextFieldKeyConfig lastFocus;
	
	protected TextFieldKeyConfig currentFocus;
	
	Image bg;
	
	protected final String[] labels = {
			"Right",
			"Left",
			"Jump",
			"Quick attack",
			"Ball",
			"Attack",
			"Projection",
			"Teleport",
			"Parry",
			"Start"
	};
	
	private Properties prop;
	
	public InputConfigurator() {
		prop = BasicActionGetterInstance.getInstance().getProperties();
		loadKeys();
		currentFocus = lastFocus = null;
	}
	
	public void loadKeys() {
		player1Bindings = new KeyBinding[10];
		player2Bindings = new KeyBinding[10];
		player1Bindings[0] = new KeyBinding("controls.p1.right", prop.getProperty("controls.p1.right"));
		player1Bindings[1] = new KeyBinding("controls.p1.left", prop.getProperty("controls.p1.left"));
		player1Bindings[2] = new KeyBinding("controls.p1.up", prop.getProperty("controls.p1.up"));
		player1Bindings[3] = new KeyBinding("controls.p1.a", prop.getProperty("controls.p1.a"));
		player1Bindings[4] = new KeyBinding("controls.p1.b", prop.getProperty("controls.p1.b"));
		player1Bindings[5] = new KeyBinding("controls.p1.x", prop.getProperty("controls.p1.x"));
		player1Bindings[6] = new KeyBinding("controls.p1.y", prop.getProperty("controls.p1.y"));
		player1Bindings[7] = new KeyBinding("controls.p1.teleport", prop.getProperty("controls.p1.teleport"));
		player1Bindings[8] = new KeyBinding("controls.p1.parry", prop.getProperty("controls.p1.parry"));
		player1Bindings[9] = new KeyBinding("controls.p1.start", prop.getProperty("controls.p1.start"));
		
		player2Bindings[0] = new KeyBinding("controls.p2.right", prop.getProperty("controls.p2.right"));
		player2Bindings[1] = new KeyBinding("controls.p2.left", prop.getProperty("controls.p2.left"));
		player2Bindings[2] = new KeyBinding("controls.p2.up", prop.getProperty("controls.p2.up"));
		player2Bindings[3] = new KeyBinding("controls.p2.a", prop.getProperty("controls.p2.a"));
		player2Bindings[4] = new KeyBinding("controls.p2.b", prop.getProperty("controls.p2.b"));
		player2Bindings[5] = new KeyBinding("controls.p2.x", prop.getProperty("controls.p2.x"));
		player2Bindings[6] = new KeyBinding("controls.p2.y", prop.getProperty("controls.p2.y"));
		player2Bindings[7] = new KeyBinding("controls.p2.teleport", prop.getProperty("controls.p2.teleport"));
		player2Bindings[8] = new KeyBinding("controls.p2.parry", prop.getProperty("controls.p2.parry"));
		player2Bindings[9] = new KeyBinding("controls.p2.start", prop.getProperty("controls.p2.start"));
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame game)
			throws SlickException {
		//super.init(gc, game);
		setPreviousState(game.getState(Menu.ID));
		loadKeys();
		font = new AngelCodeFont(C.FONTS_FOLDER + "little.fnt", C.FONTS_FOLDER + "little.png");
		player1Tf = new TextFieldKeyConfig[9];
		player2Tf = new TextFieldKeyConfig[9];
		int width =  gc.getWidth() / 6;
		int space = 300;
		int height = gc.getHeight() / 19;
		bg = new Image(C.ASSETS_FOLDER + "background.jpg");
		for(int i=0; i < player1Tf.length; i++) {
			player1Tf[i] = new TextFieldKeyConfig(gc, fontTf, space, (10 + height) * i + 150, width, height);
			player2Tf[i] = new TextFieldKeyConfig(gc, fontTf, gc.getWidth() - width * 2, (10 + height) * i + 150, width, height);
			player1Tf[i].setInputConfigurator(this);
			player2Tf[i].setInputConfigurator(this);
			player1Tf[i].setBinding(player1Bindings[i]);
			player2Tf[i].setBinding(player2Bindings[i]);
		}
	}
	
	@Override
	public void enter(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.enter(container, game);
	}

	@Override
	public int getID() {
		return 20;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sg, Graphics g)
			throws SlickException {
		super.render(gc, sg, g);
		bg.draw(0, 0, gc.getWidth(), gc.getHeight());
		g.setLineWidth(4);
		g.setFont(font);
		g.drawString("Player 1", player1Tf[0].getX(), 50);
		g.drawString("Player 2", player2Tf[0].getX(), 50);
		for (int i=0; i < player1Tf.length; i++) {
			player1Tf[i].render(gc, g);
			player2Tf[i].render(gc, g);
			g.drawString(labels[i], player1Tf[i].getX() - font.getWidth(labels[i]) - 20, player1Tf[i].getY());
			g.drawString(labels[i], player2Tf[i].getX() - font.getWidth(labels[i]) - 20, player2Tf[i].getY());
		}
	}

	@Override
	public void update(GameContainer g, StateBasedGame sg, int delta)
			throws SlickException {
		super.update(g, sg, delta);
	}
	
	@Override
	public void controllerButtonPressed(int controller, int button) {
	}
	
	public void textFieldHasChanged(TextFieldKeyConfig tf) {
		for (int i=0; i < player1Tf.length; i++) {
			if (player1Tf[i] != tf && player1Tf[i].getBinding().getValue().equals(tf.getBinding().getValue())) {
				player1Tf[i].getBinding().setValue("nop");
			}
			if (player2Tf[i] != tf && player2Tf[i].getBinding().getValue().equals(tf.getBinding().getValue())) {
				player2Tf[i].getBinding().setValue("nop");
			}
		}
	}
}
